#include "stdafx.h"
#include ".\dataqueuemgr.h"


CDataQueueMgr::CDataQueueMgr(void)
{
	m_nCurIndex = 0;
	
}

CDataQueueMgr::~CDataQueueMgr(void)
{
	Clear( );
}

DataQueueNote* CDataQueueMgr::GetNote( void )
{
	DataQueueNote* lpNode = NULL;
	if( m_vecFreeNote.empty() )
	{
		lpNode = AllocNote( );
	}
	else
	{
		lpNode = m_vecFreeNote.at( 0 );
		m_vecFreeNote.erase( m_vecFreeNote.begin() );
	}

	memset( lpNode, 0, sizeof(DataQueueNote) );

	return lpNode;

}

void CDataQueueMgr::FreeNote( DataQueueNote* lpNode )
{
	if( lpNode == NULL )
		return ;

	ASSERT( !IsBadReadPtr( lpNode, sizeof(DataQueueNote) ) );

	m_vecFreeNote.push_back( lpNode );

}

void CDataQueueMgr::Clear(void)
{
	vector<DataQueueNote*>::iterator it;	
	
	
    m_vecFreeNote.clear( );		
	
	
	for ( it = m_vecNoteArray.begin(); it != m_vecNoteArray.end(); ++it )
	{
		delete[] (*it);
	}

	m_vecNoteArray.clear();

}

DataQueueNote* CDataQueueMgr::AllocNote(void)
{
	DataQueueNote* lpList = NULL;

	DataQueueNote* lpResult = NULL;


	if( m_vecNoteArray.empty() )
	{
		lpList = new DataQueueNote[DATA_NOTE_ALLOC_COUNT];
		memset( lpList, 0, DATA_NOTE_ALLOC_COUNT*sizeof(DataQueueNote) );

		m_vecNoteArray.push_back( lpList );

		lpResult = &lpList[0];
		m_nCurIndex = 1;
	}
	else
	{
		lpList = m_vecNoteArray.at( m_vecNoteArray.size()-1 );
		lpResult = lpList+m_nCurIndex;

		m_nCurIndex++;

		if( m_nCurIndex >= DATA_NOTE_ALLOC_COUNT )
		{
			lpList = new DataQueueNote[DATA_NOTE_ALLOC_COUNT];
			memset( lpList, 0, DATA_NOTE_ALLOC_COUNT*sizeof(DataQueueNote) );

			m_vecNoteArray.push_back( lpList );
			m_nCurIndex = 0;
		}	


	}

	return lpResult;

}

int CDataQueueMgr::GetFreeNoteCount(void)
{
	return (int)m_vecNoteArray.size();
}

int CDataQueueMgr :: GetArrayCount(void)
{
	return (int)m_vecNoteArray.size();
}
